package interaction.game;

import java.awt.Rectangle;

import processing.core.PGraphics;
import processing.core.PImage;
import processing.core.PVector;
import interaction.drawable.Drawable;

public class BaseGameObject implements GameObject {
	private PImage image;
	private PVector initialPosition;
	private PVector position;
	private PVector velocity;
	private PVector size;

	public BaseGameObject(PImage image, PVector position, PVector velocity) {
		setImage(image);
		this.initialPosition = position.get();
		this.position = position.get();
		this.velocity = velocity.get();
		this.size = new PVector(image.width, image.height);
	}
	
	public void resetPosition()
	{
		position = initialPosition.get();
	}
	
	public void setImage(PImage image) {
		if (image == null) {
			throw new RuntimeException("image cannot be null");
		}
		this.image = image;
	}
	
	public PImage getImage() {
		return image;
	}
	
	public PVector getInitialPosition() {
		return initialPosition;
	}
	
	public PVector getPosition() {
		return position;
	}
	
	public PVector getSize() {
		return size;
	}
	
	public PVector getVelocity() {
		return velocity;
	}
	
	public Rectangle getBounds()
	{
		PVector position = getPosition();
		PVector size = getSize();
		return new Rectangle((int) position.x, (int) position.y, (int) size.x, (int) size.y);
	}

	public void update() {
		PVector position = getPosition();
		position.add(velocity);
	}
	
	public boolean isOnScreen(PGraphics g)
	{
		return getBounds().intersects(0, 0, g.width, g.height);
	}

	public void draw(PGraphics g) {
		PVector position = getPosition();
		g.image(image, position.x, position.y);
	}
}
